<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
    class="page-nav-bar"
    title="个人信息"
    left-arrow @click-left="$router.back()" />
    <!-- /导航栏 -->
    <input @change="onChange" type="file" hidden ref="inputFile">
    <van-cell @click="$refs.inputFile.click()" title="头像" is-link>
      <van-image
        class="avatar"
        fit="cover"
        round
        :src="list.photo"
      />
    </van-cell>
    <van-cell title="昵称" @click="showEdit=true" :value="list.name" is-link />
    <van-cell title="性别" @click="showEditSex=true" :value="list.gender?'女':'男'" is-link />
    <van-cell title="生日" @click="showEditBirthday=true" :value="list.birthday" is-link />
  <!-- 编辑昵称 -->
    <van-popup v-if="showEdit" v-model="showEdit" position="bottom" :style="{ height: '100%' }">
      <user-edit v-model="list.name" @on-change='showEdit=false' />
    </van-popup>
    <!-- 编辑性别 -->
    <van-popup v-if="showEditSex" v-model="showEditSex" position="bottom">
      <show-edit-sex v-model="list.gender" @close='showEditSex=false' />
    </van-popup>
    <!-- 编辑生日 -->
    <van-popup v-if="showEditBirthday" v-model="showEditBirthday" position="bottom">
      <user-edit-birthday v-model="list.birthday" @close='showEditBirthday=false' />
    </van-popup>
    <!-- 编辑头像 -->
    <van-popup v-if="showEditAvatar" style="height:100%" v-model="showEditAvatar" position="bottom">
      <user-edit-avatar @update-photo='list.photo = $event' @close='showEditAvatar=false' :img='imgUrl' />
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user.js'
import UserEdit from './components/UserEdit.vue'
import ShowEditSex from './components/ShowEditSex.vue'
import UserEditBirthday from './components/UserEditBirthday.vue'
import UserEditAvatar from './components/UserEditAvatar.vue'
export default {
  components: { UserEdit, ShowEditSex, UserEditBirthday, UserEditAvatar },
  data () {
    return {
      list: {},
      showEdit: false,
      showEditSex: false,
      showEditBirthday: false,
      showEditAvatar: false,
      imgUrl: ''
    }
  },

  created () {
    this.loadUserProfile()
  },

  methods: {
    async loadUserProfile () {
      try {
        const res = await getUserProfile()
        // console.log(res)
        this.list = res
      } catch (err) {}
    },
    onChange (e) {
      console.dir(e.target)
      if (e.target.value === '') return
      this.showEditAvatar = true
      this.imgUrl = URL.createObjectURL(e.target.files[0])
      e.target.value = ''
    }
  }
}
</script>

<style scoped lang='less'>
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  /deep/.van-nav-bar__arrow {
    color: #fff;
  }
}
</style>
